﻿using System;
using System.Text;
using IAM.Interface.Model;

namespace IAM.Interface.ModelExample
{
    /// <summary>
    /// 身份权限管理
    /// </summary>
    public class IdentityManagement
    {
        #region  成员方法
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="identity">人员身份信息</param>
        /// <returns>人员账号信息</returns>
        public AccountInfo CreateIdentity(Identity identity)
        {
            AccountInfo delaccountinfo = new AccountInfo();
            delaccountinfo.HRISEID = identity.Accountinfo.HRISEID;
            delaccountinfo.ADAccount = identity.Accountinfo.ADAccount;
            delaccountinfo.CSLAccount = identity.Accountinfo.CSLAccount;
            delaccountinfo.CILX500 = identity.Accountinfo.CILX500;
            delaccountinfo.UPI = identity.Accountinfo.UPI;

            BEGDC.PreContract.BLL.IEmployee employeeService = (BEGDC.PreContract.BLL.IEmployee)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];
            BEGDC.PreContract.DAL.Employee employee = employeeService.GetEmployeeByADAccount(identity.Accountinfo.ADAccount);
            if (employee == null)
            {
                employee = new BEGDC.PreContract.DAL.Employee();
                employee.Active = false;
                employee.Badge = identity.Accountinfo.HRISEID;
                employee.EName = identity.EName;
                employee.CName = identity.Name;
                employee.Email = identity.EMail;
                employee.CILAccount = identity.CILAccount;
                employee.BD = identity.BDID;
                employee.BG = identity.BGID;
                employee.Title = string.Empty;
                if (identity.ReportTo != null)
                    employee.ReportTo = identity.ReportTo.ADAccount;
                employee.UpdateDate = System.DateTime.Now;
                employee.CommentAllowed = false;
                employee.ASBOrLucent = (int)BEGDC.PreContract.BLL.Employee.ASBorLucent.All;
                employee.DownloadAllowed = false;
                employee.Phone = identity.Mobile;
                employee.Company = identity.CompanyID;
                employee.Location = identity.Location;
                employee.ADAccount = identity.Accountinfo.ADAccount;

                employee.CreateAndFlush();

                delaccountinfo.AppAccount = employee.EmployeeID.ToString();

                ResponseLocalLibrary.Business.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.IAM, "CreateIdentity:" + identity.Accountinfo.ADAccount, "IAM");

            }
            else
            {
                delaccountinfo.AppAccount = employee.EmployeeID.ToString();
            }

            identity.Accountinfo.AppAccount = delaccountinfo.AppAccount;
            this.EnableIdentity(identity);

            return delaccountinfo;
        }
        /// <summary>
        /// 激活用户
        /// </summary>
        /// <param name="identity">人员身份信息</param>
        /// <returns>人员账号信息</returns>
        public AccountInfo EnableIdentity(Identity identity)
        {
            AccountInfo delaccountinfo = new AccountInfo();
            delaccountinfo.HRISEID = identity.Accountinfo.HRISEID;
            delaccountinfo.ADAccount = identity.Accountinfo.ADAccount;
            delaccountinfo.CSLAccount = identity.Accountinfo.CSLAccount;
            delaccountinfo.CILX500 = identity.Accountinfo.CILX500;
            delaccountinfo.UPI = identity.Accountinfo.UPI;
            delaccountinfo.AppAccount = identity.Accountinfo.AppAccount;

            if (identity.Accountinfo.AppAccount == string.Empty)
            {
                throw new Exception("AppAccount can not be empty.");
            }

            BEGDC.PreContract.BLL.IEmployee employeeService = (BEGDC.PreContract.BLL.IEmployee)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];
            BEGDC.PreContract.DAL.Employee employee = employeeService.GetEmployee(Convert.ToInt32(identity.Accountinfo.AppAccount));
            if (employee != null)
            {
                employee.Active = true;
                employee.Update();

                ResponseLocalLibrary.Business.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.IAM, "EnableIdentity:" + identity.Accountinfo.ADAccount, "IAM");
            }
            else
            {
                IAM.Interface.Common.Abnormity.ResponseException exception = new IAM.Interface.Common.Abnormity.ResponseException();
                exception.StatusCode = 600;
                throw exception;
            }


            return delaccountinfo;
        }
        /// <summary>
        /// 禁用用户
        /// </summary>
        /// <param name="identity">人员身份信息</param>
        /// <returns>人员账号信息</returns>
        public AccountInfo DisableIdentity(Identity identity)
        {
            AccountInfo delaccountinfo = new AccountInfo();
            delaccountinfo.HRISEID = identity.Accountinfo.HRISEID;
            delaccountinfo.ADAccount = identity.Accountinfo.ADAccount;
            delaccountinfo.CSLAccount = identity.Accountinfo.CSLAccount;
            delaccountinfo.CILX500 = identity.Accountinfo.CILX500;
            delaccountinfo.UPI = identity.Accountinfo.UPI;
            delaccountinfo.AppAccount = identity.Accountinfo.AppAccount;

            if (identity.Accountinfo.AppAccount == string.Empty)
            {
                throw new Exception("AppAccount can not be empty.");
            }


            BEGDC.PreContract.BLL.IEmployee employeeService = (BEGDC.PreContract.BLL.IEmployee)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];
            BEGDC.PreContract.DAL.Employee employee = employeeService.GetEmployee(Convert.ToInt32(identity.Accountinfo.AppAccount));
            if (employee != null)
            {
                BEGDC.PreContract.BLL.IUserRole userroleService = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];
                userroleService.DeleteUserRoleByEmployeeId(employee.EmployeeID);

                BEGDC.PreContract.BLL.IBDAssignment bdService = (BEGDC.PreContract.BLL.IBDAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BDAssignment_Service];
                bdService.DeleteAllAssignment(employee.EmployeeID.ToString());

                BEGDC.PreContract.BLL.ICATAssignment catService = (BEGDC.PreContract.BLL.ICATAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CATAssignment_Service];
                catService.DeleteAllAssignment(employee.EmployeeID.ToString());

                BEGDC.PreContract.BLL.IProvinceAssignment provinceService = (BEGDC.PreContract.BLL.IProvinceAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ProvinceAssignment_Service];
                provinceService.DeleteAllAssignment(employee.EmployeeID.ToString());

                employee.Active = false;
                employee.Update();

                ResponseLocalLibrary.Business.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.IAM, "DisableIdentity:" + identity.Accountinfo.ADAccount, "IAM");
            }
            else
            {
                IAM.Interface.Common.Abnormity.ResponseException exception = new IAM.Interface.Common.Abnormity.ResponseException();
                exception.StatusCode = 600;
                throw exception;
            }


            return delaccountinfo;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="identity">人员身份信息</param>
        /// <returns>人员账号信息</returns>
        public AccountInfo DeleteIdentity(Identity identity)
        {
            AccountInfo delaccountinfo = new AccountInfo();
            delaccountinfo.HRISEID = identity.Accountinfo.HRISEID;
            delaccountinfo.ADAccount = identity.Accountinfo.ADAccount;
            delaccountinfo.CSLAccount = identity.Accountinfo.CSLAccount;
            delaccountinfo.CILX500 = identity.Accountinfo.CILX500;
            delaccountinfo.UPI = identity.Accountinfo.UPI;
            delaccountinfo.AppAccount = identity.Accountinfo.AppAccount;

            if (identity.Accountinfo.AppAccount == string.Empty)
            {
                throw new Exception("AppAccount can not be empty.");
            }

            BEGDC.PreContract.BLL.IEmployee employeeService = (BEGDC.PreContract.BLL.IEmployee)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];
            BEGDC.PreContract.DAL.Employee employee = employeeService.GetEmployee(Convert.ToInt32(identity.Accountinfo.AppAccount));
            if (employee != null)
            {
                BEGDC.PreContract.BLL.IUserRole userroleService = (BEGDC.PreContract.BLL.IUserRole)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_UserRole_Service];
                userroleService.DeleteUserRoleByEmployeeId(employee.EmployeeID);

                BEGDC.PreContract.BLL.IBDAssignment bdService = (BEGDC.PreContract.BLL.IBDAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BDAssignment_Service];
                bdService.DeleteAllAssignment(employee.EmployeeID.ToString());

                BEGDC.PreContract.BLL.ICATAssignment catService = (BEGDC.PreContract.BLL.ICATAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CATAssignment_Service];
                catService.DeleteAllAssignment(employee.EmployeeID.ToString());

                BEGDC.PreContract.BLL.IProvinceAssignment provinceService = (BEGDC.PreContract.BLL.IProvinceAssignment)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ProvinceAssignment_Service];
                provinceService.DeleteAllAssignment(employee.EmployeeID.ToString());

                employee.Active = false;
                employee.Update();

                ResponseLocalLibrary.Business.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.IAM, "DeleteIdentity:" + identity.Accountinfo.ADAccount, "IAM");
            }
            return delaccountinfo;
        }
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="identity">人员身份信息</param>
        /// <returns>人员账号信息</returns>
        public AccountInfo UpdateIdentity(Identity identity)
        {
            AccountInfo delaccountinfo = new AccountInfo();
            delaccountinfo.HRISEID = identity.Accountinfo.HRISEID;
            delaccountinfo.ADAccount = identity.Accountinfo.ADAccount;
            delaccountinfo.CSLAccount = identity.Accountinfo.CSLAccount;
            delaccountinfo.CILX500 = identity.Accountinfo.CILX500;
            delaccountinfo.UPI = identity.Accountinfo.UPI;
            delaccountinfo.AppAccount = identity.Accountinfo.AppAccount;

            if (identity.Accountinfo.AppAccount == string.Empty)
            {
                throw new Exception("AppAccount can not be empty.");
            }


            BEGDC.PreContract.BLL.IEmployee employeeService = (BEGDC.PreContract.BLL.IEmployee)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Employee_Service];
            BEGDC.PreContract.DAL.Employee employee = employeeService.GetEmployee(Convert.ToInt32(identity.Accountinfo.AppAccount));
            if (employee != null)
            {
                employee.Badge = identity.Accountinfo.HRISEID;
                employee.EName = identity.EName;
                employee.CName = identity.Name;
                employee.Email = identity.EMail;
                employee.CILAccount = identity.CILAccount;
                employee.BD = identity.BDID;
                employee.BG = identity.BGID;
                employee.Title = string.Empty;
                if (identity.ReportTo != null)
                    employee.ReportTo = identity.ReportTo.ADAccount;
                employee.UpdateDate = System.DateTime.Now;
                employee.CommentAllowed = false;
                employee.ASBOrLucent = (int)BEGDC.PreContract.BLL.Employee.ASBorLucent.All;
                employee.DownloadAllowed = false;
                employee.Phone = identity.Mobile;
                employee.Company = identity.CompanyID;
                employee.Location = identity.Location;
                employee.ADAccount = identity.Accountinfo.ADAccount;

                employee.Update();

                ResponseLocalLibrary.Business.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.IAM, "UpdateIdentity:" + identity.Accountinfo.ADAccount, "IAM");
            }
            else
            {
                delaccountinfo = CreateIdentity(identity);
            }

            return delaccountinfo;
        }

        #endregion  成员方法
    }
}
